package com.atguigu.mall.seckill.config;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Configuration
public class MyRabbitMQConfig {

  @Resource private RabbitTemplate rabbitTemplate;

  @Bean
  public MessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter();
  }

  @PostConstruct // MyRabbitConfig对象创建完成以后，执行这个方法
  public void initRabbitTemplate() {
    // 设置确认回调
    rabbitTemplate.setConfirmCallback(
        (correlationData, ack, cause) -> {
          System.out.println(
              "confirm...correlationData["
                  + correlationData
                  + "]==>ack:["
                  + ack
                  + "]==>cause:["
                  + cause
                  + "]");
        });

    rabbitTemplate.setReturnCallback(
        (message, replyCode, replyText, exchange, routingKey) -> {
          System.out.println(
              "Fail Message["
                  + message
                  + "]==>replyCode["
                  + replyCode
                  + "]"
                  + "==>replyText["
                  + replyText
                  + "]==>exchange["
                  + exchange
                  + "]==>routingKey["
                  + routingKey
                  + "]");
        });
  }
}
